﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DbCommandExtensions.cs" company="">
// </copyright>
// <summary>
//   DbCommand extension methods.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.Helpers.DataAccess
{
    using System;
    using System.Data;

    /// <summary>
    /// The db command extensions.
    /// </summary>
    public static class DbCommandExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// Method for creating the parameter.
        /// </summary>
        /// <param name="dbCommand">
        /// The database command.
        /// </param>
        /// <param name="parameterName">
        /// Name of the parameter.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The parameter.
        /// </returns>
        public static IDbDataParameter CreateParameter(this IDbCommand dbCommand, string parameterName, object value)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            if (string.IsNullOrEmpty(parameterName))
            {
                throw new ArgumentException("parameterName is null or empty", "parameterName");
            }

            IDbDataParameter parameter = dbCommand.CreateParameter();

            parameter.ParameterName = parameterName;
            parameter.Value = value;

            return parameter;
        }

        /// <summary>
        /// Method for creating the command parameter.
        /// </summary>
        /// <param name="dbCommand">
        /// The database command.
        /// </param>
        /// <param name="parameterName">
        /// Name of the parameter.
        /// </param>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The parameter.
        /// </returns>
        public static IDbDataParameter CreateParameter(
            this IDbCommand dbCommand, 
            string parameterName, 
            DbType type, 
            object value)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            if (string.IsNullOrEmpty(parameterName))
            {
                throw new ArgumentException("parameterName");
            }

            IDbDataParameter parameter = dbCommand.CreateParameter(parameterName, value);

            parameter.DbType = type;

            return parameter;
        }

        /// <summary>
        /// Creates the parameter.
        /// </summary>
        /// <param name="dbCommand">
        /// The database command.
        /// </param>
        /// <param name="parameterName">
        /// Name of the parameter.
        /// </param>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <param name="direction">
        /// The direction.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The parameter.
        /// </returns>
        public static IDbDataParameter CreateParameter(
            this IDbCommand dbCommand, 
            string parameterName, 
            DbType type, 
            ParameterDirection direction, 
            object value)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            if (string.IsNullOrEmpty(parameterName))
            {
                throw new ArgumentException("parameterName");
            }

            IDbDataParameter parameter = dbCommand.CreateParameter(parameterName, type, value);

            parameter.Direction = direction;

            return parameter;
        }

        /// <summary>
        /// Creates the parameter.
        /// </summary>
        /// <param name="dbCommand">
        /// The database command.
        /// </param>
        /// <param name="parameterName">
        /// Name of the parameter.
        /// </param>
        /// <param name="direction">
        /// The direction.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// The parameter.
        /// </returns>
        public static IDbDataParameter CreateParameter(
            this IDbCommand dbCommand, 
            string parameterName, 
            ParameterDirection direction, 
            object value)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            if (string.IsNullOrEmpty(parameterName))
            {
                throw new ArgumentException("parameterName");
            }

            IDbDataParameter parameter = dbCommand.CreateParameter(parameterName, value);

            parameter.Direction = direction;

            return parameter;
        }

        /// <summary>
        /// Method for creating the parameter.
        /// </summary>
        /// <param name="dbCommand">
        /// The database command.
        /// </param>
        /// <param name="parameterName">
        /// Name of the parameter.
        /// </param>
        /// <param name="direction">
        /// The direction of the parameter.
        /// </param>
        /// <returns>
        /// The parameter.
        /// </returns>
        public static IDbDataParameter CreateParameter(
            this IDbCommand dbCommand, 
            string parameterName, 
            ParameterDirection direction)
        {
            if (dbCommand == null)
            {
                throw new ArgumentNullException("dbCommand");
            }

            if (string.IsNullOrEmpty(parameterName))
            {
                throw new ArgumentException("parameterName is null or empty", "parameterName");
            }

            IDbDataParameter parameter = dbCommand.CreateParameter();

            parameter.ParameterName = parameterName;
            parameter.Direction = direction;

            return parameter;
        }

        #endregion
    }
}